package engine.newUi
{
	import engine.GameData;
	import engine.data.ModelLocator;
	import engine.data.View;
	import engine.data.ViewObject;
	import engine.display.newAttack.AttackLogic;
	import engine.flag.GameDefine;
	import engine.logic.DieHelpLogic;
	import engine.net.messages.CustomToServerFlag;
	import engine.net.mgr.SystemSender;
	
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	import flash.utils.clearTimeout;
	import flash.utils.setTimeout;
	
	import newx.com.ResManager;
	import newx.com.config.StaticIni;

	public class UpNoticeLogic
	{
		public static var _self:UpNoticeLogic;
		private var _ui:UpNoticeUI;
		/**数据模型*/
		private 	var _model			:ModelLocator;
		/**所有的提示类型**/
		private var 	_allUpType:Array;
		/**所有的场景类型**/
		private var _allSceneType:Array;
		/***当前需要显示的提示类型数组**/
		private var _curUpType:Array;
		/**当前显示的页码**/
		private var currpre:int = 0;
		/****计时器**/
		private var _timer:Timer;
		
		private var allUIArr:Array	=	[];
		/***关闭界面计时器***/
		private var _closetimer:Timer;
		
		private var _timeoutIndex:int = 0;
		public function UpNoticeLogic()
		{
		}
		
		public static function getInstance():UpNoticeLogic
		{
			if(_self == null)
			{
				_self =new UpNoticeLogic();
			}
			return _self;
		}
		               
		public function uiinit():void
		{
			_model = ModelLocator.getInstance();
//			_model.addEventListener(CustomEvent.ATTACK_STATUS,onAttackOver);
			_allUpType = StaticIni.getIniVar("allUpNotice","r").split(",");
			_allSceneType = StaticIni.getIniVar("allUpNotice","upSceneType").split(",");
		}
		
		
		/**
		 *战斗结束 
		 * @param e
		 * 
		 */
		public function onAttackOver():void
		{
			
			var curSceneType:String = GameData.SceneType.toString();
			if( !AttackLogic.getInstance().attackVo.win 
				&& _allSceneType.indexOf(curSceneType) != -1 
				&& AttackLogic.getInstance().attackVo.attackType != 3 )
			{//战斗失败且 是需要显示提示的场景//且不是PVP结算
				
				checkTSid();
				if(_curUpType.length < 1)
				{
					return;
				}
				if(!ui.created)
				{
					ui.initPosX = 0;
					ui.show();
				}
			}
			
			ui.eff.visible = true;
			ui.eff.mouseEnabled = false;
//			ui.initPosX = -40;
			ui.eff.play();
			setTimeout( function():void{ ui.eff.visible = false;}, int( StaticIni.getIniVar("allUpNotice","playTime" )) * 1000  );
		}
		
		/**
		 *检查是不是要根据特定等级显示特定的显示 
		 * 
		 */
		public function checkTSid():void
		{
			currpre = 0;
			var level:int = _model.scene.Role.getPlayerLevel();
			var levelArr:Array  = StaticIni.getIniVar("allUpNotice","tbLevel").split(",");
			var levelidArr:Array  = StaticIni.getIniVar("allUpNotice","notice").split(",");
			var index:int = levelArr.indexOf(String(level));
			var showStr:String = levelidArr[index];
			if(index != -1)
			{
				check(showStr.split("_"));
			}else
			{
				getAllType();
			}
		}
		
		
		/**
		 * 
		 * @param arr
		 * 
		 */
		private function check(arr:Array):void
		{
			var isshow:Boolean = false;
			for(var i:int = 0;i<arr.length;i++)
			{
				if(checkIDisshow(arr[i]))
				{
					showOnlyOne(arr[i]);
					isshow = true;
				    break;	
				}
			}
			if(!isshow)
			{
				getAllType();
			}
		}
		
		private function showOnlyOne(id:int):void
		{
			_curUpType = [];
			_curUpType.push("Notice_"+id);                                                                                                                                                                                                               
		}
		
		private function checkIDisshow(id:int):Boolean
		{
			
			//强化, 阵法, 武学,丹药,经脉,伙伴 武学获取,布阵,礼包
			var showindex:int = 1;
			//是不是满足开启条件
			var isChange:Boolean = false;
			var konfutype : int;
			var hbtype:int;
			switch (id)
			{
				case 1://强化
					isChange = DieHelpLogic.getInstance().equipHelp();
					break;
				case 2://阵法
					isChange = DieHelpLogic.getInstance().skillHelp();
					break;
				case 3://武学背包
					konfutype = KungfuPackLogic.getInstance().checkGuide;
					if(konfutype == DieHelpLogic.KUNGFU_EQUIP )
					{
						isChange = true;
					}
//					isChange = DieHelpLogic.getInstance().kungfuHelp();
					break;
				case 4://丹药
					isChange=IntensifyLogic.getInstance().isCanUse;
					break;
				case 5://经脉
					isChange = DieHelpLogic.getInstance().puliseHelp();
					break;
				case 6://伙伴
					
					hbtype = PartnerRequestNewLogic.getInstance().checkGuide;
					if(hbtype ==  DieHelpLogic.BUDDY)
					{
						isChange = true;
					}
//					isChange = DieHelpLogic.getInstance().buddyHelp();
					break;
				case 7://武学获取
					konfutype  = KungfuPackLogic.getInstance().checkGuide;
					if(konfutype == DieHelpLogic.KUNGFU_GET )
					{
						isChange = true;
					}
				case 8://布阵
					
					hbtype = PartnerRequestNewLogic.getInstance().checkGuide;
					if(hbtype ==  DieHelpLogic.EMBATTLE)
					{
						isChange = true;
					}
					//					showindex = DieHelpLogic.getInstance().buddyHelp();
//					showindex = 1;
					break;
				case 9://礼包
					isChange = DieHelpLogic.getInstance().giftHelp();
					break;
			}
			return isChange;
		}
		
		
		private function getAllType():void
		{
			var level:int = _model.scene.Role.getPlayerLevel();
			_curUpType = [];
			//乱序
			var tempAllArr:Array=[];
			_allUpType.forEach(function(item:*,index:int,arr:Array):void
			{
				var randomGene:int=int(Math.random()*arr.length);
				var randomIndex:int=findIndex(randomGene)
				tempAllArr[randomIndex]=item;
				function findIndex(random:int):int
				{
					if(random>=arr.length)
						return findIndex(0);
					if(tempAllArr[random])
						return findIndex(random+1);
					return random;
				}
			}
			);
			for(var i:int = 0;i<tempAllArr.length;i++)
			{
				var uptypeObj:Object = StaticIni.getIniObj(tempAllArr[i]);
				if(level >= int(uptypeObj.level))
				{
//					if( String(_allUpType[i]) == "Notice_9" )
//					{
//						if(DieHelpLogic.getInstance().giftHelp() != DieHelpLogic.GIFT)
//						{
////							_curUpType.push(_allUpType[i]);
//							continue;
//						}
//					}
					if(checkIDisshow(String(tempAllArr[i]).split("_")[1]))
					{
						_curUpType.push(tempAllArr[i]);
					}
				}
			}
			if(_curUpType.length==0)
			{
				_curUpType.push("Notice_8")
			}
		}
		
		private function showUI():void
		{
			var showObj:Object = StaticIni.getIniObj(_curUpType[currpre]);
			if(showObj == null)return;
			ui.pic.source = ResManager.newResClass(showObj.pic);
			ui.lab_name.htmlStr = StaticIni.getSwfLangStr(showObj.name);
			ui.txt_info.htmlStr = StaticIni.getSwfLangStr(showObj.info);
			ui.txt_openbtn.htmlStr	=	 "<font size='14'><a href='event:" + showObj.id + "_" +showObj.traction + "'>" + StaticIni.getSwfLangStr(showObj.linkbtn) + "</a></font>";
			if (currpre == 0)
			{
				ui.btn_pre.enabled = false;
			}else
			{
				ui.btn_pre.enabled = true;
				
			}
			
			if(currpre+1 < _curUpType.length)
			{
				ui.btn_next.enabled = true;
			}else
			{
				ui.btn_next.enabled = false;
			}
			
		}
		
		/**
		 *上一页 
		 * @param e
		 * 
		 */
		private function onbtn_preClickHandle(e:MouseEvent):void
		{
			currpre --; 
			showUI();
			_timer.reset();
		}
		
		/**
		 *下一页 
		 * @param e
		 * 
		 */
		private function onbtn_nextClickHandle(e:MouseEvent):void
		{
			currpre ++;
			showUI();
			_timer.reset();
		}
		
		public function onLinkClick(val:String):void
		{
			
			var arr:Array = val.split("_");
			
			//强化, 阵法, 武学,丹药,经脉,伙伴 武学获取,布阵,礼包
			switch (int(arr[0]))
			{
				case 1:
					if(!EquipModifiedUI.getInstance().created) EquipModifiedUI.getInstance().showOrHide();
					break;
				case 2:
					if(!StatusShopUI.getInstance().created) StatusShopUI.getInstance().showOrHide();
					break;
				
				case 3:
					if(!KungfuPackLogic.getInstance().ui.created) KungfuPackLogic.getInstance().ui.showOrHide();
					break;
				case 4:
					if(!MyPropertyLogic.getInstance().ui.created) MyPropertyLogic.getInstance().ui.showOrHide();
					if(!IntensifyUI.getInstance().created) IntensifyUI.getInstance().showOrHide();
					break;
				case 5:
					if(!PulseLogic.getInstance().ui.created) PulseLogic.getInstance().ui.show();
					break;
				case 6:
					if(!PartnerRequestNewUI.getInstance().created)  PartnerRequestNewUI.getInstance().showOrHide();
					break;
				case 7:
					DiceLogic.getInstance().ui.showOrHide();
//					MyPropertyLogic.getInstance().ui.showOrHide();
//					IntensifyUI.getInstance().showOrHide();
					break;
				case 8:
					EmbattleLogic.getInstance().ui.showOrHide();
//					SkillLogic.getInstance().showUI();
					break;
				case 9:
//					PartnerInviteLogic.getInstance().ui.showOrHide();
					
					//获取背包
					var packView : View = _model.scene.GetView( View.VIEWPORT_TOOL );
					//遍历背包，判断是否有礼包
					packView.getDataForEach( function( propName:String,val:* ):void
					{
						var canUse : Boolean = false;
						//是否有可以使用的礼包
						var item : ViewObject= val as ViewObject;
						if( null != item  )
						{
							if( item.GetPropertiesEx( "ItemType" ) == GameDefine.ItemType_GOLDBOX 
								&& item.GetPropertiesEx( "SubItemType" ) == GameDefine.SubItemType_box)
							{//是礼包
								PackLogic.getInstance().ui.showOrHide();
								SystemSender.getInstance().CustomMsg( CustomToServerFlag.CLIENT_CUSTOMMSG_USEITEM,View.VIEWPORT_TOOL, int( item.ObjKey ) );
							}
						}
					} );
					
				
				
					break;
//				default :
//					
//					SceneTransfer.getInstance().tranferTo(arr[1]);
//					break;
			}
			ui.hide();
		}
		
		private function play():void
		{
			_timer = new Timer(5000);
			_timer.addEventListener(TimerEvent.TIMER,onTimerHandle);
			_timer.start();
		}
		
		
		private function onTimerHandle(e:TimerEvent):void
		{
			if(currpre >= _curUpType.length -1)
			{
				currpre  = 0;
			}else
			{
				currpre++;
			}
			showUI();
		}
		
		public function onCloseClickHandle(e:MouseEvent):void
		{
			ui.hide();
		}
		
		public function BeforeShow():void
		{
			addListenter();
			ui.addEventListener(MouseEvent.MOUSE_OVER,onUIOverHandle);
			ui.addEventListener(MouseEvent.MOUSE_OUT,onUIoutHandle);
//			getAllType();
			if(_curUpType.length<1)
			{
				return;
			}
			showUI();
			play();
			
			allUIArr.push(_ui);
			_timeoutIndex = setTimeout(function():void
			{
				_ui.hide();
			},int(StaticIni.getIniVar("allUpNotice","closetime")));
			
		}
		
		private function onUIOverHandle(e:MouseEvent):void
		{
			clearTimeout(_timeoutIndex);
		}
		
		private function onUIoutHandle(e:MouseEvent):void
		{
			var ui:UpNoticeUI = e.currentTarget as UpNoticeUI;
			_timeoutIndex = setTimeout(
				function():void
				{
					ui.hide();
			
				},int(StaticIni.getIniVar("allUpNotice","closetime")) );
		}
		
		public function setVisible(boolean:Boolean):void
		{
			var length:int	=	allUIArr.length;
			for(var i:int=0;i<length;i++)
			{
				if(boolean)
				{
					(allUIArr[i] as UpNoticeUI).visible	=	true;
				}
				else
				{
					(allUIArr[i] as UpNoticeUI).visible	=	false;
				}
			}
		}
		
		public function AfterHide():void
		{
			currpre = 0;
			clearTimeout(_timeoutIndex);
			removeListenter();
			if(_timer)
			{
				_timer .reset();
				_timer .stop();
				_timer.removeEventListener(TimerEvent.TIMER,onTimerHandle);
				_timer = null;
			}
			var index:int	=	allUIArr.indexOf(_ui);
			if(index != -1) allUIArr.splice(index,1);
		}
		
		/**
		 *添加监听 
		 * 
		 */
		private function addListenter():void
		{
			ui.txt_openbtn.addLinkClickHandel(onLinkClick);
			ui.btn_pre.addEventListener(MouseEvent.CLICK,onbtn_preClickHandle);
			ui.btn_next.addEventListener(MouseEvent.CLICK,onbtn_nextClickHandle);
			ui.clostBtn.addEventListener(MouseEvent.CLICK,onCloseClickHandle);
		}
		
		/**
		 *移除监听 
		 * 
		 */
		private  function removeListenter():void
		{
				
		}
		
		public function get ui():UpNoticeUI
		{
			if(_ui==null)
			{
				_ui	=	new UpNoticeUI();
			}
			return _ui
		}
		
	}
}